package com.iknow.view.widget.zoom;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;
import java.util.Observable;
import java.util.Observer;

public class ImageZoomView extends View
  implements Observer
{
  private final AspectQuotient mAspectQuotient;
  private Bitmap mBitmap;
  private final Paint mPaint = new Paint(2);
  private final Rect mRectDst;
  private final Rect mRectSrc = new Rect();
  private ZoomState mState;

  public ImageZoomView(Context paramContext, AttributeSet paramAttributeSet)
  {
    super(paramContext, paramAttributeSet);
    this.mRectDst = new Rect();
    this.mAspectQuotient = new AspectQuotient();
  }

  public AspectQuotient getAspectQuotient()
  {
    return this.mAspectQuotient;
  }

  protected void onDraw(Canvas paramCanvas)
  {
    if ((this.mBitmap != null) && (this.mState != null))
    {
      float f1 = this.mAspectQuotient.get();
      int i = getWidth();
      int j = getHeight();
      int k = this.mBitmap.getWidth();
      int l = this.mBitmap.getHeight();
      float f2 = this.mState.getPanX();
      float f3 = this.mState.getPanY();
      float f4 = this.mState.getZoomX(f1) * i / k;
      float f5 = this.mState.getZoomY(f1) * j / l;
      this.mRectSrc.left = (int)(f2 * k - i / 2F * f4);
      this.mRectSrc.top = (int)(f3 * l - j / 2F * f5);
      this.mRectSrc.right = (int)(this.mRectSrc.left + i / f4);
      this.mRectSrc.bottom = (int)(this.mRectSrc.top + j / f5);
      this.mRectDst.left = getLeft();
      this.mRectDst.top = getTop();
      this.mRectDst.right = getRight();
      this.mRectDst.bottom = getBottom();
      if (this.mRectSrc.left < 0)
      {
        Rect localRect4 = this.mRectDst;
        localRect4.left = (int)(localRect4.left + f4 * -this.mRectSrc.left);
        this.mRectSrc.left = 0;
      }
      if (this.mRectSrc.right > k)
      {
        Rect localRect3 = this.mRectDst;
        localRect3.right = (int)(localRect3.right - f4 * (this.mRectSrc.right - k));
        this.mRectSrc.right = k;
      }
      if (this.mRectSrc.top < 0)
      {
        Rect localRect2 = this.mRectDst;
        localRect2.top = (int)(localRect2.top + f5 * -this.mRectSrc.top);
        this.mRectSrc.top = 0;
      }
      if (this.mRectSrc.bottom > l)
      {
        Rect localRect1 = this.mRectDst;
        localRect1.bottom = (int)(localRect1.bottom - f5 * (this.mRectSrc.bottom - l));
        this.mRectSrc.bottom = l;
      }
      paramCanvas.drawBitmap(this.mBitmap, this.mRectSrc, this.mRectDst, this.mPaint);
    }
  }

  protected void onLayout(boolean paramBoolean, int paramInt1, int paramInt2, int paramInt3, int paramInt4)
  {
    super.onLayout(paramBoolean, paramInt1, paramInt2, paramInt3, paramInt4);
    this.mAspectQuotient.updateAspectQuotient(paramInt3 - paramInt1, paramInt4 - paramInt2, this.mBitmap.getWidth(), this.mBitmap.getHeight());
    this.mAspectQuotient.notifyObservers();
  }

  public void setImage(Bitmap paramBitmap)
  {
    this.mBitmap = paramBitmap;
    this.mAspectQuotient.updateAspectQuotient(getWidth(), getHeight(), this.mBitmap.getWidth(), this.mBitmap.getHeight());
    this.mAspectQuotient.notifyObservers();
    invalidate();
  }

  public void setZoomState(ZoomState paramZoomState)
  {
    if (this.mState != null)
      this.mState.deleteObserver(this);
    this.mState = paramZoomState;
    this.mState.addObserver(this);
    invalidate();
  }

  public void update(Observable paramObservable, Object paramObject)
  {
    invalidate();
  }
}